<?php
namespace app\index\controller;
use think\Controller;
use think\Db;


class Search extends Controller
{
   
    public function index(){
        
        $Db = new Db();
        $res = $Db::table('search')->select();
        $this->assign([
            'info'=>$res
        ]);
        return $this->fetch('index');
    }

    public function getaa(){
        $data = $_POST;
        $Db = new Db();
        if(empty($data['question']) ){
            $this->error('问题不能为空!!!');
        }
        if(empty($data['answer']) ){
            $this->error('答案不能为空!!!');
        }
        $data['addtime'] = date("Y-m-d H:i",time());
        $data['updatetime'] = date("Y-m-d H:i",time());

        $res = $Db::table('search')->insert($data);
        if($res){
            $res = $Db::table('search')->select();
            $this->assign([
                'info'=>$res
            ]);
            return $this->fetch('index');
        }
        //Db::execute('insert into search (question, answer) values (?, ?)',[8,'thinkphp']);
//        print_r("<pre>") ;
//        print_r($data) ;
//        print_r("</pre>") ;
    }
    public function getimage(){
        $data = $_POST;
        $file = request()->file('image');
        $info = $file->move(ROOT_PATH . 'public/uplode');
        $aaa = $info->getSaveName();
       // $dir = $info->getFilename();
//        print_r("<pre>") ;
//        print_r($aaa) ;
//        print_r("</pre>") ;

        $datas['question'] = $data['question'];
        $datas['answer'] = $aaa;
        $datas['addtime'] = date("Y-m-d H:i",time()) ;
        $datas['updatetime'] = date("Y-m-d H:i",time());
        $Db = new Db();
        $res = $Db::table('search')->insert($datas);
        if($res){
            $res = $Db::table('search')->select();
            $this->assign([
                'info'=>$res
            ]);
            return $this->fetch('index');
        }

    }

    public function fangbian(){
        header('Content-Type:text/html; charset=utf-8');
        $data = $_POST;
        if(empty($data['question'])){
            $this->assign([
                'question'=>'憨憨输入问题呀',
                'answer'  =>'嘻嘻嘻嘻'
            ]);
            return $this->fetch('jieguo');
        }
        $Db = new Db();
        $find = $data['question'];
        $res = $Db::table('search')->where('question','like','%'.$find.'%')->select();

        if (!$res){
            $this->assign([
                'question'=>$find,
                'answer'  =>'未找到答案！！！'
            ]);
            return $this->fetch('jieguo');
        }
        $this->assign([
            'question'=>$res[0]['question'],
            'answer'  =>$res[0]['answer']
        ]);
        return $this->fetch('jieguo');
    }
    public function add(){
        return $this->fetch('add');
    }
    public function addimage(){
        return $this->fetch('addimage');
    }
    public function xiangqin(){
        $aaa = request()->param('id');
        $Db = new Db();
        $res = $Db::table('search')->where('id',$aaa)->select();
        $housanwei=strrchr($res[0]['answer'],'.');

        if($housanwei == ".jpg" || $housanwei == ".jpeg" || $housanwei ==".png" || $housanwei == ".gif" || $housanwei ==".bmp"){
            $aaa = str_replace("\\","/",$res[0]['answer']);
            $this->assign([
                'info'=>$res[0],
                'img'=>$aaa
            ]);
            return $this->fetch('tupianxiangqing');
        }else{
            $this->assign([
                'info'=>$res[0]
            ]);
            return $this->fetch('xiangqing');
        }
    }


    //接口begin
    //首页

    public function indexs(){
        $Db = new Db();
        $res = $Db::table('search')->select();
        foreach($res as $k=>$v){
            $housanwei=strrchr($v['answer'],'.');
            if($housanwei == ".jpg" || $housanwei == ".jpeg" || $housanwei ==".png" || $housanwei == ".gif" || $housanwei ==".bmp") {
                $aaa = str_replace("\\", "/", $v['answer']);
                $data[$k]['id'] = $v['id'];
                $data[$k]['question'] = $v['question'];
                $data[$k]['answer'] = 'http://heiqiqi.site/uplode/'.$aaa;
                $data[$k]['status'] = '1';
            }else{
                $data[$k]['id'] = $v['id'];
                $data[$k]['question'] = $v['question'];
                $data[$k]['answer'] = $v['answer'];
                $data[$k]['status'] = '0';
            }
        }
        $data = json_encode($data);
        echo $data;
    }
    //添加文字
    public function intozi(){
        $data = $_POST;
        $Db = new Db();
        if(empty($data['question']) ){
            $msg = '问题不能为空!!!';
            echo  json_encode($msg);
        }
        if(empty($data['answer']) ){
            $msg = '答案不能为空';
            echo  json_encode($msg);
        }
        $data['addtime'] = date("Y-m-d H:i",time());
        $data['updatetime'] = date("Y-m-d H:i",time());

        $res = $Db::table('search')->insert($data);
        if($res){
            $msg = '添加成功';
            echo  json_encode($msg);
        }
    }

    //添加图片
    public function tianajiaimage(){
        $data = $_POST;

        $file = request()->file('images');
        $info = $file->move(ROOT_PATH . 'public/uplode');
        $aaa = $info->getSaveName();
        $datas['question'] = $data['question'];
        $datas['answer'] = $aaa;
        $datas['addtime'] = date("Y-m-d H:i",time());
        $datas['updatetime'] = date("Y-m-d H:i",time());
        $Db = new Db();
        $res = $Db::table('search')->insert($datas);
        if($res){
            $msg = '添加成功';
            echo  json_encode($msg);
        }

    }

    //查询
    public function getchaxun(){
        $data = $_POST;

        $Db = new Db();
        $find = $data['question'];
        $res = $Db::table('search')->where('question','like','%'.$find.'%')->select();
        foreach($res as $k=>$v){
            $housanwei=strrchr($v['answer'],'.');
            if($housanwei == ".jpg" || $housanwei == ".jpeg" || $housanwei ==".png" || $housanwei == ".gif" || $housanwei ==".bmp") {
                $aaa = str_replace("\\", "/", $v['answer']);
                $datas[$k]['id'] = $v['id'];
                $datas[$k]['question'] = $v['question'];
                $datas[$k]['answer'] = 'http://heiqiqi.site/uplode/'.$aaa;
                $datas[$k]['status'] = '1';
            }else{
                $datas[$k]['id'] = $v['id'];
                $datas[$k]['question'] = $v['question'];
                $datas[$k]['answer'] = $v['answer'];
                $datas[$k]['status'] = '0';
            }
        }
        $datas = json_encode($datas);
        echo $datas;

    }
    //删除
    public function delect(){
        $data = $_POST;
        $Db = new Db();
        $res = $Db::table('search')->where('id',$data['id'])->delete();
        if($res){
            $msg = '删除成功';
            echo  json_encode($msg);
        }else{
            $msg = '删除失败';
            echo  json_encode($msg);
        }
    }

    public function showpic(){

    }
}
//https://gitee.com/ttacc/test.gitcd
//123132321